home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / PolyMaze 1.0.2 / PolyMaze.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-01  |  8.5 KB  |  332 lines  |  [TEXT/CWIE]

  1. // PolyMaze
  2. // version 1.0.2
  3. // ported to CodeWarrior by Ken Long <kenlong@netcom.com>
  4. // updated for CW7 on 951201
  5.  
  6. //•  ---------- "Source to 3D maze display" ---------- •//
  7.  
  8. //• B/W version written Jul 16, 1985 by Steve Hawley (sdh@joevax.UUCP)
  9. //• This is the source code for the maze display program. 
  10. //• It was done in Aztec C version 1.06D. 
  11.  
  12. //• Colorization by Kenneth A. Long, at "itty bitty bytes(tm)"
  13. //• Made to run in Metrowerks C.
  14.  
  15.  
  16. #include <stdio.h>
  17. #include <GestaltEqu.h>
  18.  
  19. #define kSleep                0L
  20.  
  21. #define mDevice                131
  22.  
  23. #define kNULLFilterProc        NULL
  24. Boolean        gDone;
  25.  
  26. WindowRecord    wRecord;
  27. WindowPtr    myWindow;
  28. EventRecord myEvent;
  29.  
  30. //• the maze. 1's = blocks, 0's = space.
  31. static char maze [22] [17] = { 
  32.     {1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1}, 
  33.     {1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1}, 
  34.     {1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1}, 
  35.     {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1}, 
  36.     {1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1}, 
  37.     {1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  38.     {1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1}, 
  39.     {1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1}, 
  40.     {1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1}, 
  41.     {1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1}, 
  42.     {1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1}, 
  43.     {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1}, 
  44.     {1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1}, 
  45.     {1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1}, 
  46.     {1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1}, 
  47.     {1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1}, 
  48.     {1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1}, 
  49.     {1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  50.     {1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1}, 
  51.     {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1}, 
  52.     {1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1}, 
  53.     {1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
  54. };        
  55.  
  56. //• Prototypes.
  57.  
  58. SetForeColor (short red, short green,short  blue);
  59. void        Maze_Window_Set(GDHandle device);
  60. Boolean        HasColorQD(void);
  61. GDHandle    How_Deep(void);
  62. short        Get_The_Depth(GDHandle device);
  63. void        Scramble_Color(RGBColor *colorPtr);
  64. int            Square_3_D(int column, int row);
  65. int            Draw_Maze(void);
  66. void        main(void);
  67.  
  68. //• Set the RGB forecolor with a single line.
  69. SetForeColor (short red, short green,short  blue)
  70. {
  71.     RGBColor theColor;
  72.  
  73.     theColor.red = red;
  74.     theColor.green = green;
  75.     theColor.blue = blue;
  76.     RGBForeColor(&theColor);
  77. }
  78.  
  79. //• Set the RGB backcolor with a single line.
  80. SetBackColor (short red, short green,short  blue)
  81. {
  82.     RGBColor theColor;
  83.  
  84.     theColor.red = red;
  85.     theColor.green = green;
  86.     theColor.blue = blue;
  87.     RGBBackColor(&theColor);
  88. }
  89.  
  90. void Maze_Window_Set (GDHandle device)
  91. {
  92.     WindowPtr    amazing;
  93.     Rect        globalRect, localRect;
  94.     
  95.     //• Set up the window.  Give it some bounds.
  96.     SetRect (&globalRect, 4, 40, 508, 370);
  97.     
  98.     amazing = NewCWindow (0L, &globalRect, "\pSomething Amazing", true, 0,
  99.                              (WindowPtr)-1L, false, 60L);
  100.     ShowWindow (amazing);
  101.     SetPort (amazing);
  102.  
  103.     //• Make a draw rect.  Since we set the port th the window, 
  104.     //• topLeft is now 0, 0 of the window (local).
  105.     //• This rect has the sole purpose of installing a background color.
  106.     //• Since we die on mouseDown, this is only done here.  No need to
  107.     //• insert preservation or update provisions.
  108.     SetRect (&localRect, 0, 0, 504, 330);
  109.     
  110.     //• So we set a back color.  Mac default is already set at white.
  111.     SetBackColor (0xdddd, 0xffff, 0xffff);
  112.     
  113.     //• Since it's white, we must erase it to reveal our RGBBackColor.
  114.     EraseRect (&localRect);
  115. }
  116.  
  117. Boolean    HasColorQD (void)
  118. {
  119.     unsigned char    version[ 4 ];
  120.     OSErr            err;
  121.     
  122.     err = Gestalt (gestaltQuickdrawVersion, (long *)version);
  123.     
  124.     if (err != noErr)
  125.     {
  126.         SysBeep (10);    //• Error.
  127.         ExitToShell ();
  128.     }
  129.     
  130.     if (version[ 2 ] > 0)
  131.         return (true);
  132.     else
  133.         return (false);
  134. }
  135.  
  136. GDHandle How_Deep (void)
  137. {
  138.     GDHandle    curDevice, maxDevice = NULL;
  139.     short        curDepth, maxDepth = 0;
  140.     
  141.     curDevice = GetDeviceList ();
  142.     
  143.     while (curDevice != NULL)
  144.     {
  145.         curDepth = Get_The_Depth (curDevice);
  146.         
  147.         if (curDepth > maxDepth)
  148.         {
  149.             maxDepth = curDepth;
  150.             maxDevice = curDevice;
  151.         }
  152.  
  153.         curDevice = GetNextDevice (curDevice);
  154.     }
  155.     
  156.     return (maxDevice);
  157. }
  158.  
  159. short Get_The_Depth (GDHandle device)
  160. {
  161.     PixMapHandle    screenPixMapH;
  162.     
  163.     screenPixMapH = (**device).gdPMap;
  164.     
  165.     return ((**screenPixMapH).pixelSize);
  166. }
  167.  
  168. //• This scramble will give a random RGB for each poly that calls it.
  169. //• I set specifics instead, but left this here for data purposes.
  170. void Scramble_Color (RGBColor *colorPtr)
  171. {
  172.     colorPtr->red =  Random () + 32767;
  173.     colorPtr->blue = Random () + 32767;
  174.     colorPtr->green = Random () + 32767;
  175. }
  176.  
  177. //• Displays a given block from the maze. Decides on which side of 
  178. //• center the block is found, and draws the visible faces, which 
  179. //• are defined as polygons. The faces are projected using the
  180. //• formulae: 
  181. //•           'x' = x / z; 
  182. //•           'y' = y / z;         
  183. //• to achieve one point perspective.
  184.  
  185. Square_3_D (int column, int row)
  186. {
  187.     RGBColor color, myColor;
  188.     int x1, x2, y1, y2, z1, z2;
  189.     PolyHandle left_side, right_side, top_side, front_side;
  190.     
  191.     x1 = (column - 8) * 100;        //• 100 is width of side.
  192.     x2 = x1 + 100;
  193.     z1 = (23 - row);
  194.     z2 = z1 -1;
  195.     y1 = 150;
  196.     y2 = 250;
  197.     
  198.     left_side = OpenPoly ();
  199.     MoveTo ((x1 / z1) + 256, (y1 / z1) + 60);
  200.     LineTo ((x1 / z1) + 256, (y2 / z1) + 60);
  201.     LineTo ((x1 / z2) + 256, (y2 / z2) + 60);
  202.     LineTo ((x1 / z2) + 256, (y1 / z2) + 60);
  203.     LineTo ((x1 / z1) + 256, (y1 / z1) + 60);
  204.     ClosePoly ();
  205.     
  206.     right_side = OpenPoly ();
  207.     MoveTo ((x2 / z1) + 256, (y1 / z1) + 60);
  208.     LineTo ((x2 / z1) + 256, (y2 / z1) + 60);
  209.     LineTo ((x2 / z2) + 256, (y2 / z2) + 60);
  210.     LineTo ((x2 / z2) + 256, (y1 / z2) + 60);
  211.     LineTo ((x2 / z1) + 256, (y1 / z1) + 60);
  212.     ClosePoly ();
  213.     
  214.     top_side = OpenPoly ();
  215.     MoveTo ((x1 / z1) + 256, (y1 / z1) + 60);
  216.     LineTo ((x2 / z1) + 256, (y1 / z1) + 60);
  217.     LineTo ((x2 / z2) + 256, (y1 / z2) + 60);
  218.     LineTo ((x1 / z2) + 256, (y1 / z2) + 60);
  219.     LineTo ((x1 / z1) + 256, (y1 / z1) + 60);
  220.     ClosePoly ();
  221.     
  222.     front_side = OpenPoly ();
  223.     MoveTo ((x1 / z2) + 256, (y1 / z2) + 60);
  224.     LineTo ((x2 / z2) + 256, (y1 / z2) + 60);
  225.     LineTo ((x2 / z2) + 256, (y2 / z2) + 60);
  226.     LineTo ((x1 / z2) + 256, (y2 / z2) + 60);
  227.     LineTo ((x1 / z2) + 256, (y1 / z2) + 60);
  228.     ClosePoly ();
  229.     
  230.     //• Decide to draw right side (dark gray).
  231.     if (x2 < 0 && maze [row] [column + 1] != 1)
  232.     {
  233. //        Scramble_Color (&color);
  234. //        RGBForeColor (&color);
  235.  
  236.         //• Uncomment the above two lines 
  237.         //• and comment out the below line,
  238.         //• and do the same for similar sets below for UGLY random.
  239.  
  240.         //• 4444 is 5/16 of the range.
  241.         SetForeColor (0x4444, 0x4444, 0x4444);
  242.         ErasePoly (right_side);
  243.         PaintPoly (right_side);
  244.         SetForeColor (0, 0, 0);        //• Our line is "off" or black.
  245.         FramePoly (right_side);
  246.     }
  247.     
  248.     //• Decide to draw left face (light gray).
  249.     if (x1 > 0 && maze [row] [column - 1] != 1) 
  250.     {
  251. //        Scramble_Color (&color);
  252. //        RGBForeColor (&color);
  253.         SetForeColor (0xbbbb, 0xbbbb, 0xbbbb);    //• 11/16 toward full on.
  254.         ErasePoly (left_side);
  255.         PaintPoly (left_side);
  256.         
  257.         SetForeColor (0, 0, 0);
  258.         FramePoly (left_side);
  259.     }
  260.  
  261.     //• Draw the top_side (white).
  262. //    Scramble_Color (&color);
  263. //    RGBForeColor (&color);
  264.     SetForeColor (0xeeee, 0xeeee, 0xeeee);    //• 15/16 full on.
  265.     ErasePoly (top_side);
  266.     PaintPoly (top_side);
  267.     SetForeColor (0, 0, 0);
  268.     FramePoly (top_side);
  269.     
  270.     //• Draw the front_side (gray).
  271. //    Scramble_Color (&color);
  272. //    RGBForeColor (&color);
  273.     SetForeColor (0x7777, 0x7777, 0x7777);    //• 7/16 full on.
  274.     ErasePoly (front_side);
  275.     PaintPoly (front_side);
  276.     
  277.     SetForeColor (0, 0, 0);
  278.     FramePoly (front_side);
  279.     
  280.     //• Restore pen characteristics.
  281.     PenNormal (); 
  282.     
  283.     //• Dispose of all the polygons to free up memory.
  284.     KillPoly (top_side);        
  285.     KillPoly (front_side);
  286.     KillPoly (left_side);
  287.     KillPoly (right_side);
  288. }
  289.  
  290. //• Draws out all the blocks, starting from the back, moving left 
  291. //• to center, then right to center.
  292.  
  293. Draw_Maze ()
  294. {
  295.     Rect trect;
  296.     int row, column;
  297.     
  298.     for (row = 0; row < 22; row++)
  299.     {
  300.         for (column = 0; column < 8; column++)
  301.             if (maze [row] [column] == 1) 
  302.                 Square_3_D (column, row);
  303.         for (column = 16; column > 7; column--)
  304.             if (maze [row] [column] == 1) 
  305.                 Square_3_D (column, row);
  306.     }
  307. }
  308.  
  309. void    main (void)
  310. {
  311.     InitGraf (&qd.thePort);
  312.     InitFonts ();
  313.     InitWindows ();
  314.     InitMenus ();
  315.     TEInit ();
  316.     InitDialogs (nil);
  317.     InitCursor ();
  318.  
  319.     //• No color?  Then you don't get to play!
  320.     if (! HasColorQD ())
  321.         ExitToShell ();
  322.             
  323.     //• HowDeep is not really needed, since all our colors are set
  324.     //• at divisions of 16.  But in case you need it, it was left in.
  325.     Maze_Window_Set (How_Deep ());    
  326.     
  327.     Draw_Maze ();
  328.     
  329.     while (!Button ())     //• Click and exit.
  330.         GetNextEvent (everyEvent, &myEvent);
  331. }
  332.